package 力扣;

public class 移除元素 {
    public static void main(String[] args) {
       int[] nums={0,1,2,2,3,0,4,2};
       int val=2;
        int i = removeElement(nums, val);
        System.out.println(i);
    }
    public static int removeElement(int[] nums, int val) {
        int i = 0;
        int n = nums.length;
        while (i < n) {
            if (nums[i] == val) {
                //把当前值 = 最后一个值  再次比较
                //不计较顺序
                nums[i] = nums[n - 1];
                n--;
            } else {
                i++;
            }
        }
        return n;
    }
}
